<template>
  <div>
    <div class="todo-container">
      <div class="todo-wrap">
        <AddHeader @addTodo="addTodo"/>
        <DisplayList :todos="todos"/>
        <Obliterate :todos="todos" :checkAllTodo="checkAllTodo" :clearAllTodo="clearAllTodo"/>
      </div>
    </div>
  </div>
</template>

<script>
import AddHeader from "./componebts/AddHeader.vue";
import DisplayList from "./componebts/DisplayList.vue";
import Obliterate from "./componebts/Obliterate.vue";

export default {
  name: "App",
  data() {
    return {
      todos: [
        {titio: "55", id: "000", done: false},
        {titio: "33", id: "001", done: false}
      ]
    };
  },
  mounted() {
    this.$bus.$on("checkTod", this.checkTod);
    this.$bus.$on("removeBtn", this.removeBtn);
    this.$bus.$on("updatoTodo", this.updatoTodo);
  },
  beforeDestroy() {
    this.$bus.$off("checkTod");
    this.$bus.$off("removeBtn");
    this.$bus.$off("updatoTodo");
  },
  methods: {
    //添加数据
    addTodo(todoObj) {
      this.todos.unshift(todoObj);
    },
    //选择或取消todo
    checkTod(id) {
      this.todos.forEach(todo => {
        if (todo.id == id) todo.done = !todo.done;
      });
    },
    //删除一个todo
    removeBtn(id) {
      this.todos.forEach((todo) => {
        if (todo.id == id) this.todos.splice(id, 1);
      });
    },
    //全部 选择或取消todo
    checkAllTodo(done) {
      this.todos.forEach(todo => {
        todo.done = done;
      });
    },
    //清除所有已完成
    clearAllTodo() {
      this.todos = this.todos.filter(todo => {
        return !todo.done;
      });
    },
    //更新一个todo
    updatoTodo(id, titio) {
      this.todos.forEach(todo => {
        if (todo.id == id) todo.titio = titio;
      });
    },
  },
  components: {AddHeader, DisplayList, Obliterate}
};
//90
</script>

<style>
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
  0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-edit {
  color: #fff;
  background-color: #0d87e7;
  border: 1px solid #085d9f;
  margin-right: 5px;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}
</style>

<style scoped>
/*app*/
.todo-container {
  width: 600px;
  margin: 0 auto;
}

.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>
